package cn.hubujyjs.lp.service.impl;

import cn.hubujyjs.lp.bo.StudentTaskBo;
import cn.hubujyjs.lp.config.UserHolder;
import cn.hubujyjs.lp.domain.Task;
import cn.hubujyjs.lp.domain.User;
import cn.hubujyjs.lp.service.TaskService;
import cn.hubujyjs.lp.service.UserService;
import cn.hubujyjs.lp.utils.R;
import cn.hubujyjs.lp.vo.StudentTaskVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hubujyjs.lp.domain.StudentTask;
import cn.hubujyjs.lp.service.StudentTaskService;
import cn.hubujyjs.lp.mapper.StudentTaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author admin
* @description 针对表【student_task】的数据库操作Service实现
* @createDate 2023-05-16 16:50:28
*/
@Service
public class StudentTaskServiceImpl extends ServiceImpl<StudentTaskMapper, StudentTask>
    implements StudentTaskService{
    @Autowired
    private TaskService taskService;
    @Autowired
    private UserService userService;
    @Override
    public R<List<StudentTaskVo>> result(StudentTaskBo studentTaskBo) {
        Long id = UserHolder.getUser().getId();
        LambdaQueryWrapper<StudentTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentTask::getUserId,id);
        List<StudentTask> list = this.list(wrapper);
        if (list==null || list.size()==0){
            return R.ok();
        }
        List<StudentTaskVo> studentTaskVos = list.stream().map(i -> {
            Integer taskId = i.getTaskId();
            Task byId = taskService.getById(taskId);
            Integer createUser = byId.getCreateUser();
            User byId1 = userService.getById(createUser);
            String name = byId1.getName();
            return StudentTaskVo.builder()
                    .taskNo(byId.getTaskNo())
                    .state(i.getStatus())
                    .taskDoc(byId.getTaskDoc())
                    .createUsername(name)
                    .deadline(byId.getDeadline())
                    .submitFile(i.getSubmitFile())
                    .createTime(byId.getCreateTime())
                    .taskTerminal(byId.getTaskTerminal())
                    .build();
        }).toList();
        return R.ok(studentTaskVos);
    }
}




